package cn.operation.backend.service;

import cn.operation.backend.enums.DeleteStatusEnum;
import cn.operation.base.mapper.BmsReceivableBillMapper;
import cn.operation.base.mapper.ItfLatestCodePondMapper;
import cn.operation.base.model.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReceivableDetailServiceImpl implements ReceivableDetailService{
    private static final Logger logger = LoggerFactory.getLogger(ReceivableDetailServiceImpl.class);
    @Autowired
    private BmsReceivableBillMapper bmsReceivableBillMapper;
    @Autowired
    private ItfLatestCodePondMapper itfLatestCodePondMapper;
    /**
     * 根据账单号，获取应收账单明细信息。
     * @param billCode
     * @return
     * @throws Exception
     */
    @Override
    public ReceivableDetailDO getReceivableDetailByBillCode(String billCode) throws Exception{
       logger.info("ReceivableDetailServiceImpl.getReceivableDetailByBillCode  param {}",billCode);
        ReceivableDetailDO receivableDetailByBillCode=new ReceivableDetailDO();
       try {
           receivableDetailByBillCode = bmsReceivableBillMapper.getReceivableDetailByBillCode(billCode);
       }catch (Exception e){
           logger.error("ReceivableDetailServiceImpl.getReceivableDetailByBillCode error 据获取应收账单明细信息失败",e);
           throw new Exception(e);
       }
        try {
            ItfLatestCodePondExample example = new ItfLatestCodePondExample();
            example.createCriteria().andCodeEqualTo(billCode).andStatusEqualTo(DeleteStatusEnum.NORMAL.getValue());
            List<ItfLatestCodePond> list = itfLatestCodePondMapper.selectByExample(example);
            if (list.size()>0) {
                ItfLatestCodePond codePond = new ItfLatestCodePond();
                codePond.setId(list.get(0).getId());
                codePond.setStatus(DeleteStatusEnum.DELETRE.getValue());
                itfLatestCodePondMapper.updateByPrimaryKeySelective(codePond);
            }
        } catch (Exception e) {
            logger.error("ReceivableDetailServiceImpl.getReceivableDetailByBillCode param update ItfLatestCodePond data error: ");
            throw new IllegalArgumentException("修改业务单据号码池单据状态为删除状态异常:"+ e);
        }
        return receivableDetailByBillCode;
    }

    /**
     * 修改开票状态
     * @param bmsReceivableBill
     * @return
     * @throws Exception
     */
    @Override
    public int submitInvoiced(BmsReceivableBill bmsReceivableBill ) throws Exception{
        logger.info("ReceivableDetailServiceImpl.submitInvoiced param {}",bmsReceivableBill);
        int i;
        try{
            String invoiceNo = bmsReceivableBill.getInvoiceNo();
            if(invoiceNo.trim().equals("{invoiceNo}")){
                invoiceNo="(null)";
            }
            i = bmsReceivableBillMapper.submitInvoiced(bmsReceivableBill.getBillCode(),bmsReceivableBill.getInvoiceDate(),invoiceNo);
            if (i !=1){
                throw new  Exception("开票状态更改失败");
            }
        }catch (Exception e){
            logger.error("ReceivableDetailServiceImpl.submitInvoiced error",e);
            throw new Exception(e.getMessage());
        }
        return i;
    }
}
